using System;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using TShooter.TfsWord.Resources;
using TShooter.TfsWord.Win.WizardSupport.Properties;

namespace TShooter.TfsWord.Win.WizardSupport.Design
{
    /// <summary>
    /// Summary description for WizardDesigner.
    /// </summary>
    public class WizardDesigner : ParentControlDesigner
    {
        private bool _AllowGrid = true;

        /// <summary>
        /// Prevents the grid from being drawn on the Wizard
        /// </summary>
        protected override bool DrawGrid
        {
            get { return base.DrawGrid && _AllowGrid; }
        }

        public override DesignerVerbCollection Verbs
        {
            get
            {
                var verbs = new DesignerVerbCollection
                                {
                                    new DesignerVerb(Strings.AddStartFinishPage, handleAddInfoPage),
                                    new DesignerVerb(Strings.AddPageWithHeader, handleAddPageWithHeader),
                                    new DesignerVerb(Strings.AddBlankPage, handleAddPage)
                                };

                return verbs;
            }
        }

        /// <summary>
        /// Simple way to ensure <see cref="WizardPage"/>s only contained here
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public override bool CanParent(Control control)
        {
            if (control is WizardPage)
            {
                return true;
            }
            return false;
        }

        public override bool CanParent(ControlDesigner controlDesigner)
        {
            if (controlDesigner is WizardPageDesigner)
            {
                return true;
            }
            return false;
        }


        protected override bool GetHitTest(Point point)
        {
            var wizard = (Wizard) Control;

            if (wizard.NextButton.Enabled &&
                wizard.NextButton.ClientRectangle.Contains(wizard.NextButton.PointToClient(point)))
            {
                //Next can handle that
                return true;
            }
            if (wizard.BackButton.Enabled &&
                wizard.BackButton.ClientRectangle.Contains(wizard.BackButton.PointToClient(point)))
            {
                //Back can handle that
                return true;
            }
            //Nope not interested
            return false;
        }

        private void handleAddPage(object sender, EventArgs e)
        {
            var wizard = (Wizard) Control;

            var designerHost = (IDesignerHost) GetService(typeof (IDesignerHost));
            var componentChangeService = (IComponentChangeService) GetService(typeof (IComponentChangeService));
            var designerTransaction = designerHost.CreateTransaction(Strings.AddBlankPage);
            var page = (WizardPage) designerHost.CreateComponent(typeof (WizardPage));

            componentChangeService.OnComponentChanging(wizard, null);

            //Add a new page to the collection
            
            wizard.Pages.Add(page);
            wizard.Controls.Add(page);
            wizard.ActivatePage(page);

            componentChangeService.OnComponentChanged(wizard, null, null, null);
            designerTransaction.Commit();
        }

        private void handleAddPageWithHeader(object sender, EventArgs e)
        {
            var wizard = (Wizard) Control;
            var designerHost = (IDesignerHost) GetService(typeof (IDesignerHost));
            var componentChangeService = (IComponentChangeService) GetService(typeof (IComponentChangeService));
            var designerTransaction = designerHost.CreateTransaction(Strings.AddPageWithHeader);
            var page = (WizardPage) designerHost.CreateComponent(typeof (WizardPage));
            var header = (Header) designerHost.CreateComponent(typeof (Header));

            componentChangeService.OnComponentChanging(wizard, null);

            page.Controls.Add(header);
            header.Dock = DockStyle.Top;

            //Add a new page to the collection
            
            wizard.Pages.Add(page);
            wizard.Controls.Add(page);
            wizard.ActivatePage(page);

            componentChangeService.OnComponentChanged(wizard, null, null, null);
            designerTransaction.Commit();
        }

        private void handleAddInfoPage(object sender, EventArgs e)
        {
            var wizard = (Wizard) Control;
            var designerHost = (IDesignerHost) GetService(typeof (IDesignerHost));
            var componentChangeService = (IComponentChangeService) GetService(typeof (IComponentChangeService));
            var designerTransaction = designerHost.CreateTransaction(Strings.AddStartFinishPage);
            var page = (WizardPage) designerHost.CreateComponent(typeof (InfoPage));

            componentChangeService.OnComponentChanging(wizard, null);

            //Add a new page to the collection
            
            wizard.Pages.Add(page);
            wizard.Controls.Add(page);
            wizard.ActivatePage(page);

            componentChangeService.OnComponentChanged(wizard, null, null, null);
            
            designerTransaction.Commit();
        }

        protected override void OnPaintAdornments(PaintEventArgs pe)
        {
            _AllowGrid = false;
            base.OnPaintAdornments(pe);
            _AllowGrid = true;

//			if (base.DrawGrid)
//			{
//				Wizard wiz = this.Control as Wizard;
//				Brush brush = new HatchBrush(HatchStyle.Percent10,SystemColors.ControlText, wiz.BackColor);
//
//				pe.Graphics.FillRectangle(brush,0,0,wiz.Width,8);
//				pe.Graphics.FillRectangle(brush,0,0,8,wiz.pnlButtons.Top);
//				pe.Graphics.FillRectangle(brush,0,wiz.pnlButtons.Top-8,wiz.Width,8);
//				pe.Graphics.FillRectangle(brush,wiz.Width-8,0,8,wiz.pnlButtons.Top);
//			}		
        }
    }
}